package tv.dyndns.kishibe.server.relevance;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.ngram.NGramTokenizer;

import tv.dyndns.kishibe.server.util.Normalizer;

import com.google.common.io.CharStreams;

public class NGramAnalyzer extends Analyzer {
	private static final int MAX_NGRAM_WEIGHT = 4;
	private static final int MIN_NGRAM_WEIGHT = 1;

	@Override
	public TokenStream tokenStream(String fieldName, Reader reader) {
		String s = null;
		try {
			s = CharStreams.toString(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new NGramTokenizer(new StringReader(Normalizer.normalize(s)), MIN_NGRAM_WEIGHT,
				MAX_NGRAM_WEIGHT);
	}
}
